package com.ruoyi.web.controller.module.active;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.active.CommunityActiveSign;
import com.ruoyi.system.service.module.active.ICommunityActiveService;
import com.ruoyi.system.service.module.active.ICommunityActiveSignService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 活动报名名单Controller
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Validated
@Api(value = "活动报名名单控制器", tags = {"活动报名名单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/active/sign")
public class CommunityActiveSignController extends BaseController {

    private final ICommunityActiveSignService communityActiveSignService;
	private final ICommunityActiveService communityActiveService;

    /**
     * 查询活动报名名单列表
     */
    @ApiOperation("查询活动报名名单列表")
    @PreAuthorize("@ss.hasPermi('module:activeSign:list')")
    @GetMapping("/list")
    public TableDataInfo<CommunityActiveSign> list(@Validated CommunityActiveSign communityActiveSign) {
        QueryWrapper<CommunityActiveSign> qw = new QueryWrapper<>();
		if(communityActiveSign.getActiveId()!= null){
			qw.eq("active_id", communityActiveSign.getActiveId());
		}

		if(StrUtil.isNotEmpty(communityActiveSign.getRealName())){
			qw.and(wrapper->{
				wrapper.like("real_name", communityActiveSign.getRealName()).or().like("phone_num", communityActiveSign.getRealName());
			});
		}
		qw.orderByDesc("create_time");

        TableDataInfo<CommunityActiveSign> td = PageUtils.buildDataInfo(communityActiveSignService.page(PageUtils.buildPage(),qw));
        List<CommunityActiveSign> list = td.getRows();
        return td;
    }

    /**
     * 导出活动报名名单列表
     */
    @ApiOperation("导出活动报名名单列表")
    @PreAuthorize("@ss.hasPermi('module:activeSign:export')")
    @Log(title = "活动报名名单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<CommunityActiveSign> export(@Validated CommunityActiveSign communityActiveSign) {
		QueryWrapper<CommunityActiveSign> qw = new QueryWrapper<>();
		if(communityActiveSign.getActiveId()!= null){
			qw.eq("active_id", communityActiveSign.getActiveId());
		}

		if(StrUtil.isNotEmpty(communityActiveSign.getRealName())){
			qw.and(wrapper->{
				wrapper.like("real_name", communityActiveSign.getRealName()).or().like("phone_num", communityActiveSign.getRealName());
			});
		}
		qw.orderByDesc("create_time");
        List<CommunityActiveSign> list = communityActiveSignService.list(qw);
		if (list.size()>0){
			for (CommunityActiveSign list1:list){
				list1.setActiveName(communityActiveService.findById(communityActiveSign.getActiveId()).getActiveTitle());
			}

		}
        ExcelUtil<CommunityActiveSign> util = new ExcelUtil<CommunityActiveSign>(CommunityActiveSign.class);
        return util.exportExcel(list, "活动报名名单");
    }

    /**
     * 获取活动报名名单详细信息
     */
    @ApiOperation("获取活动报名名单详细信息")
    @PreAuthorize("@ss.hasPermi('module:activeSign:query')")
    @GetMapping("/{id}")
    public AjaxResult<CommunityActiveSign> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        CommunityActiveSign communityActiveSign = communityActiveSignService.getById(id);
        return AjaxResult.success(communityActiveSign);
    }

    /**
     * 新增活动报名名单
     */
    @ApiOperation("新增活动报名名单")
    @PreAuthorize("@ss.hasPermi('module:activeSign:add')")
    @Log(title = "活动报名名单", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody CommunityActiveSign communityActiveSign) {
        return toAjax(communityActiveSignService.save(communityActiveSign) ? 1 : 0);
    }

    /**
     * 修改活动报名名单
     */
    @ApiOperation("修改活动报名名单")
    @PreAuthorize("@ss.hasPermi('module:activeSign:edit')")
    @Log(title = "活动报名名单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody CommunityActiveSign communityActiveSign) {
        return toAjax(communityActiveSignService.updateById(communityActiveSign) ? 1 : 0);
    }

    /**
     * 删除活动报名名单
     */
    @ApiOperation("删除活动报名名单")
    @PreAuthorize("@ss.hasPermi('module:activeSign:remove')")
    @Log(title = "活动报名名单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(communityActiveSignService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
